package itsm.isperp.module.controller.bpm; 

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.bpm.BpmNodeCategoryRelation;
import itsm.isperp.module.service.bpm.BpmNodeCategoryRelationService;
import itsm.isperp.workflow.graph.Node;
import itsm.isperp.workflow.service.BpmProcessDefinitaionService;
import itsm.isperp.workflow.service.BpmProcessRunService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 控制器
 * 
 * @author lizx
 * @date 2016-09-05 11:21
 */
@Controller
@RequestMapping("bpm/nodeCategoryRelation")
public class BpmNodeCategoryRelationController extends BaseController<BpmNodeCategoryRelation> {

	@Autowired
	protected BpmNodeCategoryRelationService bpmNodeCategoryRelationService;

	@Override
	public BpmNodeCategoryRelationService getService() {
		return this.bpmNodeCategoryRelationService;
	}
	
	@Override
	public String getPageTitle() {
		return "流程节点数据权限控制";
	}
	
	
	
	@RequestMapping("getCannotWriteColumnsAndDefaultValue")
	@ResponseBody
	public Map<String,Object> getCannotWriteColumnsAndDefaultValue(String status,String categoryId,String id, JqGridRequest req) {
		/*BpmProcessRunService bpmProcessRunService = ContextHolder
				.getSpringBean("bpmProcessRunService");*/
		//String curNodeId=bpmProcessRunService.getActId(null, processKey, processId);
		//req.setSearchString(" bpm_sid_id='"+curNodeId+"' and category_id='"+categoryId+"'");
		req.setSelectColumns("can_not_write","required_write");
		req.setSearchString(" can_not_write_status like '%"+status+"%' and category_id='"+categoryId+"'");
		BpmNodeCategoryRelation data1=this.getService().findOneByRequest(req);
		Map<String,Object> m=new HashMap<String,Object>();
		m.put("prvColumnsSetting", data1);
		String sql1="SELECT * FROM idd_category_property a WHERE default_value<>'' and a.category_id='"+categoryId+"' AND default_value IS NOT NULL AND NOT EXISTS (SELECT 1 FROM idd_individuation_property WHERE (VALUE<>'' OR VALUE IS NOT NULL) AND category_property_id=a.id AND relation_id='"+id+"')";
		JdbcTemplate jdbcTemplate = ContextHolder
				.getSpringBean("jdbcTemplate");
		List<Map<String,Object>> data2=jdbcTemplate.queryForList(sql1);
		m.put("defaultColumnValueList", data2);
		return m;
	}
	
	
	
	@RequestMapping("edit")
	@Override
	public ModelAndView edit(String id, HttpServletRequest req) {
		ModelAndView mv = new ModelAndView();
		BpmNodeCategoryRelation model=null;
		if (StringUtils.isEmpty(id)) {
			model = new BpmNodeCategoryRelation();
		}else
		{
			model = this.getService().findOne(id); 
		}
		mv.addObject("model", model);
		mv.setViewName(this.getModulePath() + "/edit");
		return mv;
	}
	
	
}
